package solution;

import solution.bean.ListNode;


/*
* 24
*
* */
public class SwapPairs {
    /*
    * 使用递归
    * */
    public ListNode solution(ListNode head) {
        if (head == null || head.next == null){
            return head;
        }

        ListNode newHead = head.next;
        head.next = solution(newHead.next);
        newHead.next = head;
        return newHead;
    }

    /*
    * 使用迭代，前后指针
    * */
    public ListNode solution2(ListNode head){
        ListNode dummyHead = new ListNode(0);
        dummyHead.next = head;
        ListNode temp = dummyHead;
        while (temp.next != null && temp.next.next != null){
            ListNode node1 = temp.next;
            ListNode node2 = temp.next.next;
            temp.next = node2;
            node1.next = node2.next;
            node2.next = node1;
            temp = node1;
        }
        return dummyHead.next;
    }

    /*
    * 可以使用Stack
    * */
}
